/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package login;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDesktopPane;
import main.ConnectionSigleton;
import utils.UtilsWindow;

/**
 *
 * @author linux
 */
public class LoginFrameController {
    private static LoginFrameController instance;
    private LoginFrame lFrame;
    
    public static LoginFrameController getInstance(){
        if(instance == null){
            instance = new LoginFrameController();
        }
        return instance;
    }

    public LoginFrame getlFrame() {
        return lFrame;
    }
    
    
    
    public void cleanFields(){
        lFrame.setUserName("");
        
    }
    
    public void login(String nickname){
        String msg = "login\n"+nickname+"\n\n";
        try {

            ConnectionSigleton.getInstance().writeSocket(msg);
        } catch (IOException ex) {
            Logger.getLogger(LoginFrameController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void show(JDesktopPane desktop) {
        if (lFrame == null){
            lFrame = new LoginFrame();
            desktop.add(lFrame);
            //limipiarCamposPaciente(); 
            UtilsWindow.getCenterWindowCoordinates(desktop,lFrame); 
            cleanFields();
            lFrame.setVisible(true);
            
        }else {
            if(lFrame.isVisible()) {
                lFrame.toFront();
                lFrame.pack();
            }else {
                if(!desktop.isAncestorOf(lFrame)){
                    desktop.add(lFrame);
                }
                //limipiarCamposPaciente(); 
                UtilsWindow.getCenterWindowCoordinates(desktop,lFrame);              
                cleanFields();
                lFrame.setVisible(true);
            }
        }

    }
}
